package com.algo.panelmn;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.reduce.LongSumReducer;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class CookieUV extends Configured implements Tool {
	public static class Mapper extends org.apache.hadoop.mapreduce.Mapper<Text, Text, Text, LongWritable>{
		///LongWritable one = new LongWritable(1);
		public void map(Text key, Text value, Context context)
				throws IOException, InterruptedException {
			long freq = Long.parseLong(value.toString());
			String id = key.toString();
			String[] items = id.split("\\^");
			if(items.length == 3){
				if(items[1].equals("locu")){
					for(int i = 1; i <= freq && i <= 10;i++)
						context.write(new Text(items[0] + ",uuid," + i + "+"), new LongWritable(1));
				}
				else if(items[1].equals("locf")){
					for(int i = 1; i <= freq && i <= 10;i++)
						context.write(new Text(items[0] + ",usns," + i + "+"), new LongWritable(1));
				}
				else if(items[1].equals("locs")){
					for(int i = 1; i <= freq && i <= 10;i++)
						context.write(new Text(items[0] + ",sns," + i + "+"), new LongWritable(1));
				}
				else
					System.out.println("unvalid key appeared");
			}
			else if(items.length == 4){
				if(items[1].equals("locu")){
					for(int i = 1; i <= freq && i <= 10;i++)
						context.write(new Text(items[0] + "," + items[3] + ",uuid," + i + "+"), new LongWritable(1));
				}
				else if(items[1].equals("locf")){
					for(int i = 1; i <= freq && i <= 10;i++)
						context.write(new Text(items[0] + "," + items[3] + ",usns," + i + "+"), new LongWritable(1));
				}
				else if(items[1].equals("locs")){
					for(int i = 1; i <= freq && i <= 10;i++)
						context.write(new Text(items[0] + "," + items[3] + ",sns," + i + "+"), new LongWritable(1));
				}
				else
					System.out.println("unvalid key appeared");
			}
		}
	}
	public static void main(String[] args) throws Exception {
		int res = ToolRunner.run(new Configuration(), new CookieUV(), args);
		System.exit(res);
	}
	public int run(String[] args) throws Exception {
		for(int i = 0; i< args.length; i++)
			System.out.println("step3:" + args[i]);
		Configuration conf = getConf();
		Job job = new Job(conf, "CookieUV");
		job.setJarByClass(CookieUV.class);
		
		job.setMapperClass(Mapper.class);
		job.setReducerClass(LongSumReducer.class);
		
		job.setInputFormatClass(KeyValueTextInputFormat.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(LongWritable.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(LongWritable.class);
		
		TextOutputFormat.SEPERATOR = ",";
		job.setOutputFormatClass(TextOutputFormat.class);
		
		
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		job.setNumReduceTasks(3);	
		return job.waitForCompletion(true) ? 0 : 1;
	}
}
